package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.RepairBbsType;
import cn.cs.service.RepairBbsTypeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/repairBbsType")
public class RepairBbsTypeController {

    @Resource
    private RepairBbsTypeService repairBbsTypeService;

    /**
     * 分页查询 论坛分类
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getRepairBbsTypeByPage")
    public Dto getRepairBbsTypeByPage(Integer pageNumber, Integer pageSize) throws Exception {
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", repairBbsTypeService.queryRepairBbsTypePageByMap(param, pageNumber, pageSize));
    }

    /**
     * 获取所有帖子分类
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/p/getRepairBbsTypeAll")
    public Dto getRepairBbsTypeAll() throws Exception {
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", repairBbsTypeService.getRepairBbsTypeListByMap(param));
    }

    /**
     * 新增/编辑 论坛分类
     * @param repairBbsType
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateRepairBbsType")
    public Dto updateRepairBbsType(@RequestBody RepairBbsType repairBbsType) {
        try {
            String msg = "新增成功";
            repairBbsType.setTypeCode(PingYinUtil.getFirstSpell(repairBbsType.getTypeName()));
            if (EmptyUtils.isEmpty(repairBbsType.getId())) {
                repairBbsType.setId(IdWorker.getId());
                repairBbsTypeService.addRepairBbsType(repairBbsType);
            } else {
                msg = "修改成功";
                repairBbsTypeService.modifyRepairBbsType(repairBbsType);
            }
            return DtoUtil.returnSuccess(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 删除论坛分类
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delRepairBbsType")
    public Dto delRepairBbsType(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += repairBbsTypeService.deleteRepairBbsTypeById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("删除失败，此分类下已经有帖子，无法删除", "001");
        }
    }

}
